package com.atguigu.base;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author lhh
 * @ClassName BaseController
 * @date 2022/9/14 14:42
 */

public class BaseController {

    protected Map<String, Object> getFilters(HttpServletRequest request) {
        Enumeration<String> paramNames = request.getParameterNames();
        Map<String, Object> filters = new TreeMap();
        while(paramNames != null && paramNames.hasMoreElements()) {
            String paramName = (String)paramNames.nextElement();
            String[] values = request.getParameterValues(paramName);
            if (values != null && values.length != 0) {
                if (values.length > 1) {
                    filters.put(paramName, values);
                } else {
                    filters.put(paramName, values[0]);
                }
            }
        }
        if(!filters.containsKey("pageNum")) {
            filters.put("pageNum", 1);
        }
        if(!filters.containsKey("pageSize")) {
            filters.put("pageSize", 5);
        }

        return filters;

        /*
        //获取前端传的参数
        Enumeration parameterNames = request.getParameterNames();
        //建立一个TreeMap（），存储对应的有序键值对
        TreeMap<String, Object> filters = new TreeMap<String, Object>();
        while (parameterNames != null && parameterNames.hasMoreElements()) {
            String paramName = (String) parameterNames.nextElement();
            String[] values = request.getParameterValues(paramName);
            if(values !=null && values.length!=0){
                if(values.length>1){
                    filters.put(paramName,values);
                }else {
                    filters.put(paramName,values[0]);
                }
            }
        }

        if(!filters.containsKey("pageNum")){
            filters.put("pageNum",1);
        }
        if(!filters.containsKey("pageSize")){
            filters.put("pageSize",5);
        }

        return filters;
        */
    }
}
